<template>
  <my-layout sTitle="搜索">
    <div slot="content">
      <div class="search-wrapper-la" v-show="netWork">
        <div class="search-cont">
          <div class="search-inputs">
            <span @click="hideOn">
              {{typeMsg}}
              <img src="@/assets/images/subIcon.png" alt>
            </span>
            <input type="text" v-model="wordKey" placeholder="搜索商品或楼盘">
          </div>
          <div class="dropdown" v-show="isShowMenu">
            <p @click="chooseType(i)" v-for="(item,i) in type" :key="i">{{item}}</p>
          </div>
          <span @click="searchEvent" class="search-btn">搜索</span>
        </div>
        <div class="ls" v-show="isShowLs">
          <div class="ls-title-box">
            <span class="ls-title">历史搜索</span>
            <span class="clear" @click="clearBtn">清除全部</span>
          </div>
          <div class="ls-list">
            <span class="list-icon" v-for="(item,i) in recordList" :key="i">{{item}}</span>
          </div>
        </div>
        <div class="find">
          <div class="find-title">
            <span>搜索发现</span>
          </div>
          <div class="find-list">
            <span
              class="find-list-icon"
              v-for="(item,i) in hot"
              @click="goPage(item)"
              :key="i"
            >{{item.name}}</span>
          </div>
        </div>
      </div>
      <div class="no-network" v-show="!netWork">
        <div class="search-logo">
          <img src="@/assets/images/wlcopy.png" alt>
        </div>
        <div class="search-txt">网络出错了哦！点击进行重连</div>
        <div class="new-ld-btn">重新加载</div>
      </div>
    </div>
  </my-layout>
</template>

<script>
export default {
  data() {
    return {
      wordKey: "",
      typeMsg: "商品",
      type: ["商品", "楼盘"],
      recordList: [],
      hot: {},
      isShowMenu: false, // 下来菜单
      isShowLs: false, // 搜索历史
      netWork: true
    };
  },
  methods: {
    async getKey() {
      const { data } = await this.$http.post("word/key");
      if (data.code == 0) {
        this.hot = data.keys;
      }
    },
    goPage(item) {
      if (item.type == "goods") {
        this.$router.push({
          path: "/goods",
          query: {
            wordKey: item.name
          }
        });
      } else {
        this.$router.push({
          path: "/floor",
          query: {
            wordKey: item.name
          }
        });
      }
    },
    hideOn() {
      this.isShowMenu = !this.isShowMenu;
    },
    chooseType(i) {
      this.isShowMenu = !this.isShowMenu;
      this.typeMsg = this.type[i];
    },
    clearBtn() {
      this.isShowLs = false;
      localStorage.clear("key");
    },
    // 搜索按钮
    searchEvent() {
      // 存储搜索记录
      let key = localStorage.getItem("key");
      key = key ? JSON.parse(key) : [];
      key.length < 5 ? key : key.shift();
      key.push(this.wordKey);
      localStorage.setItem("key", JSON.stringify(key));
      if (this.typeMsg == "商品") {
        this.$router.push({
          path: "/goods",
          query: {
            wordKey: this.wordKey
          }
        });
      } else {
        this.$router.push({
          path: "/floor",
          query: {
            wordKey: this.wordKey
          }
        });
      }
    }
  },
  mounted() {
    this.getKey();
    let key = localStorage.getItem("key");
    this.recordList = key != null ? JSON.parse(key).reverse() : [];
    this.isShowLs = this.recordList.length ? true : false;
  }
};
</script>

<style lang="scss">
.search-wrapper-la {
  padding: 0 48px;
  padding-bottom: 300px;
  .search-cont {
    position: relative;
    .search-inputs {
      width: 900px;
      line-height: 80px;
      height: 80px;
      background: #f7f7f7;
      margin-top: 40px;
      border-radius: 10px;
      font-size: 28px;
      color: #333333;
      > span {
        margin-left: 40px;
        margin-right: 66px;
        i {
          img {
            vertical-align: middle;
            margin-left: 16px;
          }
        }
      }
      input {
        border: none;
        line-height: 80px;
        background: none;
        outline: none;
        width: 620px;
      }
    }
    .search-btn {
      color: #5252a1;
      position: absolute;
      font-size: 32px;
      right: -20px;
      top: 22px;
    }
    .dropdown {
      position: absolute;
      top: 70px;
      left: 35px;
      z-index: 10;
      padding: 30px 0;
      margin: 5px 0;
      width: 100px;
      font-size: 26px;
      text-align: center;
      background-color: #fff;
      border: 1px solid #ebeef5;
      color: #606266;
      border-radius: 4px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      &::after {
        content: "";
        display: block;
        position: absolute;
        width: 20px;
        height: 20px;
        background: #fff;
        top: -10px;
        transform: rotate(45deg);
        left: 50%;
        margin-left: -10px;
      }
      p {
        line-height: 50px;
      }
      p:hover {
        background-color: #ecf5ff;
        color: #66b1ff;
      }
    }
  }
  .ls {
    margin-top: 90px;
    .ls-title-box {
      .ls-title {
        color: #333;
        font-size: 36px;
      }
      .clear {
        float: right;
        font-size: 28px;
        color: #9b9b9b;
        cursor: pointer;
      }
    }
    .ls-list {
      overflow: hidden;
      .list-icon {
        height: 60px;
        line-height: 60px;
        border-radius: 10px;
        background: #f7f7f7;
        padding: 0 38px;
        float: left;
        font-size: 30px;
        margin-right: 30px;
        margin-top: 32px;
      }
    }
  }
  .find {
    margin-top: 80px;
    .find-title {
      > span {
        color: #333;
        font-size: 36px;
      }
    }
    .find-list {
      overflow: hidden;
      .find-list-icon {
        height: 60px;
        line-height: 60px;
        border-radius: 10px;
        background: rgba(108, 134, 236, 0.4);
        padding: 0 38px;
        float: left;
        color: #5252a1;
        font-size: 30px;
        margin-right: 30px;
        margin-top: 32px;
      }
    }
  }
}
.no-network {
  position: absolute;
  height: 100%;
  background: #f7f7f7;
  top: 126px;
  left: 0;
  right: 0;
  bottom: 0;
  .search-logo {
    margin: 360px 360px 80px 360px;
    img {
      width: 360px;
      height: 252px;
    }
  }
  .search-txt {
    text-align: center;
    color: #333;
    font-size: 32px;
  }
  .new-ld-btn {
    width: 480px;
    height: 80px;
    background: #5252a1;
    border-radius: 30px;
    text-align: center;
    line-height: 80px;
    color: #fff;
    font-size: 32px;
    font-weight: 500;
    margin: 0 auto;
    margin-top: 350px;
  }
}
</style>
